package com.sky.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.*;
import com.sky.entity.*;
import com.sky.exception.OrderBusinessException;
import com.sky.mapper.*;
import com.sky.result.PageResult;
import com.sky.service.OrderService;
import com.sky.utils.HttpClientUtil;
import com.sky.utils.WeChatPayUtil;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import com.sky.websocket.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private AddressBookMapper addressBookMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private WeChatPayUtil weChatPayUtil;

    @Autowired
    private WebSocketServer webSocketServer;

    @Value("${sky.shop.address}")
    private String shopAddress;  //店铺地址

    @Value("${sky.shop.ak}")
    private String ak;  //百度地图应用服务ak

    @Value("${sky.shop.distance}")
    private Integer distance;  //配送距离上限

    //地理编码接口，获取地址的经纬度坐标
    private static final String GEO_CODING_URL = "https://api.map.baidu.com/geocoding/v3/";
    //路线规划接口，骑行路线规划，（可以视业务情况选择骑行，公交，驾车等路线，接口不一样，可以配置在yml中）
    private static final String DIRECTION_URL = "https://api.map.baidu.com/directionlite/v1/riding?";

    /**
     * 用户下单（隐性的意思是新增订单）
     *
     * @param dto
     * @return
     */
    @Transactional  //开启事务
    public OrderSubmitVO submit(OrdersSubmitDTO dto) {
        //粗粒度分析:
        //获取当前登录人id
        Long userId = BaseContext.getCurrentId();

        //根据用户主键查询用户信息
        User user = userMapper.getById(userId);
        if (user == null) {
            throw new OrderBusinessException(MessageConstant.USER_NOT_LOGIN);
        }


        //根据地址簿id查询地址信息
        AddressBook addressBook = addressBookMapper.getById(dto.getAddressBookId());
        if (addressBook == null) {
            throw new OrderBusinessException(MessageConstant.ADDRESS_BOOK_IS_NULL);
        }

        //根据用户id查询购物车数据
        List<ShoppingCart> cartList = shoppingCartMapper.getByUserId(userId);
        //判断购车为空的情况要报异常
        if (cartList == null || cartList.size() == 0) {
            throw new OrderBusinessException(MessageConstant.SHOPPING_CART_IS_NULL);
        }

        //优化下单逻辑：需要检验配送范围（如果超过5公里，就抛出自定义异常）
        if (!checkOutRange(addressBook.getProvinceName() + addressBook.getCityName() + addressBook.getDistrictName() + addressBook.getDetail())) {
            throw new OrderBusinessException(MessageConstant.OUT_OF_RANGE);
        }

        //构造订单数据对象orders，将orders插入到orders表中
        Orders orders = new Orders();
        BeanUtils.copyProperties(dto, orders);

        //需要补充属性字段
        orders.setNumber(System.currentTimeMillis() + "");  //订单号:使用系统当前时间 转换成订单号
        orders.setStatus(Orders.PENDING_PAYMENT); //订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消 7退款
        orders.setUserId(userId);                 //用户id
        orders.setOrderTime(LocalDateTime.now());  //下单时间
        orders.setPayStatus(Orders.UN_PAID);        //支付状态 0未支付 1已支付 2退款
        orders.setPhone(addressBook.getPhone());    //联系人手机号
        orders.setAddress(addressBook.getDetail());  //收货地址，外卖配送范围都是比较的小的，所以省略省市
        orders.setConsignee(addressBook.getConsignee());    //收货人
        orders.setUserName(user.getName());     //下单人

        orderMapper.insert(orders);
        log.info("订单id：{}", orders.getId());

        //构造订单明细列表数据orderDetails，将orderDetails插入到order_details表中
        //根据购物车的商品数据，构造订单明细。
        List<OrderDetail> orderDetails = new ArrayList<>();
        cartList.forEach(cart -> {
            //蒋购物车对象的数据拷贝给订单明细对象
            OrderDetail orderDetail = new OrderDetail();
            BeanUtils.copyProperties(cart, orderDetail, "id");
            //关联订单id
            orderDetail.setOrderId(orders.getId());
            orderDetails.add(orderDetail);
        });

        //调用mapper层方法，批量插入数据
        orderDetailMapper.insertBatch(orderDetails);

        //清空购物车
        shoppingCartMapper.deleteByUserId(userId);

        //构造vo对象兵返回
        return OrderSubmitVO.builder()
                .id(orders.getId())
                .orderAmount(orders.getAmount())
                .orderTime(orders.getOrderTime())
                .orderNumber(orders.getNumber())
                .build();
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    public OrderPaymentVO payment(OrdersPaymentDTO ordersPaymentDTO) throws Exception {
       /* //当前登录用户id
       Long userId = BaseContext.getCurrentId();
       User user = userMapper.getById(userId);

       //调用微信支付接口，生成预支付交易单
       JSONObject jsonObject = weChatPayUtil.pay(
               ordersPaymentDTO.getOrderNumber(), //商户订单号
               new BigDecimal(0.01), //支付金额，单位 元
               "苍穹外卖订单", //商品描述
               user.getOpenid() //微信用户的openid
       );

       if (jsonObject.getString("code") != null && jsonObject.getString("code").equals("ORDERPAID")) {
           throw new OrderBusinessException("该订单已支付");
       }

       OrderPaymentVO vo = jsonObject.toJavaObject(OrderPaymentVO.class);
       vo.setPackageStr(jsonObject.getString("package"));*/
        //业务处理，修改订单状态，来单提醒
        paySuccess(ordersPaymentDTO.getOrderNumber());

        return new OrderPaymentVO();
    }

    /**
     * 支付成功，修改订单状态
     *
     * @param outTradeNo
     */
    public void paySuccess(String outTradeNo) {
        // 当前登录用户id
        Long userId = BaseContext.getCurrentId();

        // 根据订单号查询当前用户的订单
        Orders ordersDB = orderMapper.getByNumberAndUserId(outTradeNo, userId);

        // 根据订单id更新订单的状态、支付方式、支付状态、结账时间
        Orders orders = Orders.builder()
                .id(ordersDB.getId())
                .status(Orders.TO_BE_CONFIRMED)
                .payStatus(Orders.PAID)
                .checkoutTime(LocalDateTime.now())
                .build();

        orderMapper.update(orders);

        //支付成功之后，需要出发来单提醒
        Map paramMap=new HashMap();
        paramMap.put("type",1);
        paramMap.put("orderId",orders.getId());
        paramMap.put("content","您有新的订单待处理"+ordersDB.getNumber());
        String message=JSON.toJSONString(paramMap);
        //调用webSocket，主动推送消息给客户端
        webSocketServer.sendToAllClient(message);
    }


    /**
     * 校验配送范围
     *
     * @return
     */
    private boolean checkOutRange(String address) {
        //获取店铺的地址坐标,使用Map集合存储地址信息
        //https://api.map.baidu.com/geocoding/v3/?address=北京市海淀区上地十街10号&output=json&ak=您的ak
        HashMap<String, String> paramMap = new HashMap<>();
        paramMap.put("address", this.shopAddress);
        paramMap.put("output", "json");
        paramMap.put("ak", this.ak);
        String jsonStr = HttpClientUtil.doGet(GEO_CODING_URL, paramMap);
        JSONObject jsonObject = JSON.parseObject(jsonStr);//把字符串转换成json格式
        if (!jsonObject.get("status").equals(0)) {//地址解析失败
            throw new OrderBusinessException(MessageConstant.ADDRESS_PARSE_FAILED);
        }
        JSONObject jsonObject1 = jsonObject.getJSONObject("result").getJSONObject("location");
        Object lng = jsonObject1.get("lng"); //经度
        Object lat = jsonObject1.get("lat");//纬度
        String origin = lat + "," + lng;
        log.info("店铺的坐标：{}", origin);


        //获取收货地址的坐标
        paramMap.put("address", address);
        jsonStr = HttpClientUtil.doGet(GEO_CODING_URL, paramMap);
        jsonObject = JSON.parseObject(jsonStr);//把字符串转换成json格式
        if (!jsonObject.get("status").equals(0)) {//地址解析失败
            throw new OrderBusinessException(MessageConstant.ADDRESS_PARSE_FAILED);
        }
        jsonObject1 = jsonObject.getJSONObject("result").getJSONObject("location");
        lng = jsonObject1.get("lng"); //经度
        lat = jsonObject1.get("lat");//纬度
        String destination = lat + "," + lng;
        log.info("收货地址的坐标：{}", destination);


        //3，根据店铺和收货地址坐标，进行距离计算，如果超出5公里，就返回false
        //https://api.map.baidu.com/directionlite/v1/riding?origin=40.01116,116.339303&destination=39.936404,116.452562&ak=您的AK
        paramMap.put("origin", origin);
        paramMap.put("destination", destination);
        jsonStr = HttpClientUtil.doGet(DIRECTION_URL, paramMap);
        jsonObject = JSON.parseObject(jsonStr);//把字符串转换成json格式
        if (!jsonObject.get("status").equals(0)) {
            throw new OrderBusinessException(MessageConstant.ADDRESS_PARSE_FAILED);
        }
        JSONArray routes = jsonObject.getJSONObject("result").getJSONArray("routes");
        Integer distance = (Integer) routes.getJSONObject(0).get("distance");
        log.info("配送距离：distance:{}米", distance);


        return distance <= this.distance;
    }

    /**
     * 用户端订单分页查询
     *
     * @param page
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PageResult page(Integer page, Integer pageSize, Integer status) {
        //设置分页
        PageHelper.startPage(page, pageSize);

        OrdersPageQueryDTO ordersPageQueryDTO = new OrdersPageQueryDTO();
        ordersPageQueryDTO.setUserId(BaseContext.getCurrentId());
        ordersPageQueryDTO.setStatus(status);

        //分页条件查询
        Page<Orders> p = orderMapper.pageQuery(ordersPageQueryDTO);


        return null;
    }

    /**
     * 查询订单详情
     *
     * @param id
     * @return
     */
    @Override
    public OrderVO details(Long id) {
        //根据id查询订单
        Orders orders = orderMapper.getById(id);

        //查询该订单对应的菜品、套餐明细
        List<OrderDetail> orderDetailList = orderDetailMapper.getByOrderId(orders.getId());

        //将该订单及其详情封装到ordervo并返回
        OrderVO orderVO = new OrderVO();
        BeanUtils.copyProperties(orders, orderVO);
        orderVO.setOrderDetailList(orderDetailList);

        return orderVO;
    }

    /**
     * 取消订单
     *
     * @param id
     * @throws Exception
     */
    @Override
    public void userCancelById(Long id) throws Exception {
        //根据id查询订单
        Orders ordersDB = orderMapper.getById(id);

        //校验订单是否存在
        if (ordersDB == null) {
            throw new OrderBusinessException(MessageConstant.ORDER_NOT_FOUND);
        }

        //订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
        if (ordersDB.getStatus() > 2) {
            throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
        }

        Orders orders = new Orders();
        orders.setId(ordersDB.getId());

        //订单处于待接单状态下取消，需要进行退款
        if (ordersDB.getStatus().equals(Orders.TO_BE_CONFIRMED)) {
            //调用微信支付退款接口--由于没有可用的商户id，下面退款操作执行会失败，直接注释，假设退款成功，直接修改状态
            //weChatPayUtil.refund(
            //        ordersDB.getNumber(),  //商户订单号
            //        ordersDB.getNumber(),   //商户订单号
            //        new BigDecimal(0.01),   //退款金额，单位 元
            //        new BigDecimal(0.01)); //原订单金额

            //支付状态修改为 退款
            orders.setPayStatus(Orders.REFUND);
        }
        //更新订单状态，取消原因，取消时间
        orders.setStatus(Orders.CANCELLED);
        orders.setCancelReason("用户取消");
        orders.setCancelTime(LocalDateTime.now());
        orderMapper.update(orders);
    }

    /**
     * 再来一单
     *
     * @param id
     */
    @Override
    public void repetition(Long id) {
        //查询当前用户id
        Long userId = BaseContext.getCurrentId();

        //根据订单id查询当前订单详情
        List<OrderDetail> orderDetailList = orderDetailMapper.getByOrderId(id);

        //将订单详情对象转换为购物车对象
        List<ShoppingCart> shoppingCartList = orderDetailList.stream().map(x -> {
            ShoppingCart shoppingCart = new ShoppingCart();

            //将原订单详情里面的菜品信息重新复制到购物车对象中
            BeanUtils.copyProperties(x, shoppingCart, "id");
            shoppingCart.setUserId(userId);
            shoppingCart.setCreateTime(LocalDateTime.now());

            return shoppingCart;
        }).collect(Collectors.toList());

        //将购物车对象批量添加到数据库中
        shoppingCartMapper.insertBatch(shoppingCartList);
    }

    /**
     * 条件搜索订单
     *
     * @param ordersPageQueryDTO
     * @return
     */
    @Override
    public PageResult conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        //设置分页
        PageHelper.startPage(ordersPageQueryDTO.getPage(), ordersPageQueryDTO.getPageSize());
        Page<Orders> page = orderMapper.pageQuery(ordersPageQueryDTO);

        //部分订单状态，需要额外返回订单菜品信息，将orders转化韦orderVO
        List<OrderVO> orderVOList = getOrderVOList(page);
        return new PageResult(page.getTotal(), orderVOList);

    }


    private List<OrderVO> getOrderVOList(Page<Orders> page) {
        //需要返回订单菜品信息，自定义OrderVO响应结果
        List<OrderVO> orderVOList = new ArrayList<>();
        List<Orders> ordersList = page.getResult();
        if (!CollectionUtils.isEmpty(ordersList)) {
            for (Orders orders : ordersList) {
                //将共同字段复制到OrderVO
                OrderVO orderVO = new OrderVO();
                BeanUtils.copyProperties(orders, orderVO);
                String orderDishes = getOrderDishesStr(orders);
                //将订单菜品信息封装到orderVO中，并添加到orderVOList
                orderVO.setOrderDishes(orderDishes);
                orderVOList.add(orderVO);
            }
        }
        return orderVOList;
    }

    /**
     * 根据订单id获取菜品信息字符串
     *
     * @param orders
     * @return
     */
    private String getOrderDishesStr(Orders orders) {
        //查询订单菜品详情信息（订单中的菜品和数量）
        List<OrderDetail> orderDetailList = orderDetailMapper.getByOrderId(orders.getId());

        //将每一条订单菜品信息拼接为字符串（格式：宫保鸡丁*3）
        List<String> orderDishList = orderDetailList.stream().map(x -> {
            String orderDish = x.getName() + "*" + x.getNumber() + ";";
            return orderDish;
        }).collect(Collectors.toList());

        //将该订单对应的所有菜品信息拼接在一起
        return String.join("", orderDishList);
    }


    /**
     * 各个状态的订单数量统计
     *
     * @return
     */
    @Override
    public OrderStatisticsVO statistics() {
        //根据状态，分别查询出待接单，待派送，派送中的订单数量
        Integer toBeConfirmed = orderMapper.countStatus(Orders.TO_BE_CONFIRMED);
        Integer confirmed = orderMapper.countStatus(Orders.CONFIRMED);
        Integer deliveryInProgress = orderMapper.countStatus(Orders.DELIVERY_IN_PROGRESS);

        //将查询出的数据封装到orderStatisticsVO中响应。
        OrderStatisticsVO orderStatisticsVO = new OrderStatisticsVO();
        orderStatisticsVO.setToBeConfirmed(toBeConfirmed);
        orderStatisticsVO.setConfirmed(confirmed);
        orderStatisticsVO.setDeliveryInProgress(deliveryInProgress);

        return orderStatisticsVO;
    }

    /**
     * 接单
     *
     * @param ordersConfirmDTO
     */
    @Override
    public void confirm(OrdersConfirmDTO ordersConfirmDTO) {
        Orders orders = Orders.builder()
                .id(ordersConfirmDTO.getId())
                .status(Orders.CONFIRMED)
                .build();
        orderMapper.update(orders);
    }

    /**
     * 拒单
     *
     * @param ordersRejectionDTO
     * @throws Exception
     */
    @Override
    public void rejection(OrdersRejectionDTO ordersRejectionDTO) throws Exception {
        //根据id查询订单
        Orders ordersDB = orderMapper.getById(ordersRejectionDTO.getId());

        //订单只有存在且状态为2（待接单）才可以拒单
        if (ordersDB == null || !ordersDB.getStatus().equals(Orders.TO_BE_CONFIRMED)) {
            throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
        }

        //支付状态
        Integer payStatus = ordersDB.getPayStatus();
        if (payStatus.equals(Orders.PAID)) {
            //用户已经支付，需要退款
            String refund = weChatPayUtil.refund(
                    ordersDB.getNumber(),
                    ordersDB.getNumber(),
                    new BigDecimal("0.01"),
                    new BigDecimal("0.01"));
            log.info("申请退款：{}", refund);
        }

        //拒单需要退款，根据订单id更新订单状态，拒单原因，取消时间
        Orders orders = new Orders();
        orders.setId(ordersDB.getId());
        orders.setStatus(Orders.CANCELLED);
        orders.setRejectionReason(ordersRejectionDTO.getRejectionReason());
        orders.setCancelTime(LocalDateTime.now());

        orderMapper.update(orders);
    }

    /**
     * 取消订单
     * @param ordersCancelDTO
     * @throws Exception
     */
    @Override
    public void cancel(OrdersCancelDTO ordersCancelDTO) throws Exception {
        //根据id查询订单
        Orders ordersDB = orderMapper.getById(ordersCancelDTO.getId());

        //支付状态
        Integer payStatus = ordersDB.getPayStatus();
        if (payStatus == 1) {
            //用户已之后，需要退款
            String refund = weChatPayUtil.refund(
                    ordersDB.getNumber(),
                    ordersDB.getNumber(),
                    new BigDecimal("0.01"),
                    new BigDecimal("0.01"));
            log.info("申请退款：{}", refund);
        }

        //管理端取消订单需要退款，根据订单id更新状态，取消原因，取消时间
        Orders orders = new Orders();
        orders.setId(ordersCancelDTO.getId());
        orders.setStatus(Orders.CANCELLED);
        orders.setCancelReason(ordersCancelDTO.getCancelReason());
        orders.setCancelTime(LocalDateTime.now());

        orderMapper.update(orders);
    }

    /**
     * 派送订单"
     * @param id
     */
    @Override
    public void delivery(Long id) {
        //根据id查询订单
        Orders ordersBD = orderMapper.getById(id);

        //校验订单是否存在，并且状态为3
        if (ordersBD==null || !ordersBD.getStatus().equals(Orders.CONFIRMED)){
            throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
        }

        Orders orders = new Orders();
       orders.setId(ordersBD.getId());
       //更新订单状态，状态转为配送中
        orders.setStatus(Orders.DELIVERY_IN_PROGRESS);

        orderMapper.update(orders);
    }

    /**
     * 完成订单
     * @param id
     */
    @Override
    public void complete(Long id) {
        //根据id查询订单
        Orders ordersBD = orderMapper.getById(id);

        //校验订单是否存在，并且状态为3
        if (ordersBD==null || !ordersBD.getStatus().equals(Orders.CONFIRMED)){
            throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
        }

        Orders orders = new Orders();
        orders.setId(ordersBD.getId());
        //更新订单状态，状态转为完成
        orders.setStatus(Orders.COMPLETED);
        orders.setDeliveryTime(LocalDateTime.now());

        orderMapper.update(orders);
    }

    /**
     * 客户催单
     * @param id
     */
    @Override
    public void reminder(Long id) {
        //根据订单id查询订单
        Orders orders = orderMapper.getById(id);
        if (orders==null){
            throw new OrderBusinessException(MessageConstant.ORDER_NOT_FOUND);
        }

        //1，构造JSON响应数据
        Map paramMap=new HashMap();
        paramMap.put("type",2);
        paramMap.put("orderId",id);
        paramMap.put("content",orders.getNumber());

        //2，调用webSocket方法，主动推送消息给前端
        webSocketServer.sendToAllClient(JSON.toJSONString(paramMap));
    }
}
